package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.view.CheckView;
import com.zcx.helper.view.rebound.ReboundLayout;
import com.zcx.helper.view.rebound.ReboundListView;
import com.zcx.helper.widget.AppReboundAdapter;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.activity.ConfirmOrderActivity;
import com.zcx.qshop.conn.JsonAddressAsyGet;
import com.zcx.qshop.view.SelectView;

import java.util.List;

/**
 * Created by Administrator on 2015/8/19 0019.
 */
public abstract class AddressManageAdapter extends AppReboundAdapter<JsonAddressAsyGet.Info.Address> {

    private LayoutInflater layoutInflater;

    public AddressManageAdapter(Context context, ReboundListView reboundListView,  List<JsonAddressAsyGet.Info.Address> objects) {

        super(context, reboundListView, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public ReboundLayout getReboundLayout(int i, ReboundLayout reboundLayout, ViewGroup viewGroup) {

        final JsonAddressAsyGet.Info.Address address = getItem(i);

        ViewHolder viewHolder;

        if(reboundLayout == null){

            reboundLayout = (ReboundLayout) BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_address_manage, null)));

            reboundLayout.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) reboundLayout.getTag();

        }

        viewHolder.selectView.setOnCheckChangeListener(new CheckView.OnCheckChangeListener() {

            @Override
            public void onCheckChange(View view, boolean b) {

                onSelectChange(address);

            }

        });

        if(ConfirmOrderActivity.onOrderChangeListener != null){

            viewHolder.selectView.setVisibility(View.GONE);

        }else{

            viewHolder.selectView.setCheck(getItem(i).type.equals("1"), false);

        }

        viewHolder.name.setText(getItem(i).name);

        viewHolder.phone.setText(getItem(i).mobile);

        viewHolder.address.setText(getItem(i).city);

        viewHolder.location.setText(getItem(i).address);

        return reboundLayout;

    }

    private class ViewHolder{

        @BoundView(R.id.item_address_manage_select) public SelectView selectView;

        @BoundView(R.id.item_address_manage_name) public TextView name;

        @BoundView(R.id.item_address_manage_phone) public TextView phone;

        @BoundView(R.id.item_address_manage_address) public TextView address;

        @BoundView(R.id.item_address_manage_location) public TextView location;

    }

    protected abstract void onSelectChange(JsonAddressAsyGet.Info.Address address);

}
